<?php


namespace app\index\model;
use think\Db;
use think\Model;
//用户收货地址
//定义类型
class UserAddress extends Model
{
    /**查找指定用户收货地址
     * @param int $uId
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function selAddress(int $uId){
        // TODO 请统一使用model类，不使用助手函数
        $data = $this
            ->table('userAddress')
            ->field('provide,city,county,street,detail,name,sex,phone,isDefault')
            ->where('userId','=', $uId)
            ->select();
        return $data;
    }

    /**
     * @param int $userId
     * @return mixed
     */
    //找出用户地址信息
    public function findUserAddressInfo(int $userId){
        return $this->table('userAddress')->field('id,provide,city,county,street,detail,name,sex,phone,isDefault')->where('userId',$userId)->select();
    }


    /**
     * @param int $userId
     * @param string $provide
     * @param string $city
     * @param string $county
     * @param string $street
     * @param string $detail
     * @param string $name
     * @param string $sex
     * @param string $phone
     * @return int|string
     */
    //添加用户收货地址
    public function insertAddress(int $userId,string $provide,string $city,string $county,string $street,string $detail,string $name,string $sex,string $phone){
        $data = [
            'userId' => $userId,
            'provide' => $provide,
            'city' => $city,
            'county' => $county,
            'street' => $street,
            'detail' => $detail,
            'name' => $name,
            'sex' => $sex,
            'phone' => $phone,
            'createTime' => time(),
            'updateTime' => time()
        ];
        return $this->table('userAddress')->insert($data);
    }

    /**
     * @param int $id
     * @param string $provide
     * @param string $city
     * @param string $county
     * @param string $street
     * @param string $detail
     * @param string $name
     * @param string $sex
     * @param string $phone
     * @return mixed
     */
    //更改用户地址
    public function updateAddress(int $id,int $userId,string $provide,string $city,string $county,string $street,string $detail,string $name,string $sex,string $phone){
        $data = [
            'id' => $id,
            'provide' => $provide,
            'city' => $city,
            'county' => $county,
            'street' => $street,
            'detail' => $detail,
            'name' => $name,
            'sex' => $sex,
            'phone' => $phone,
            'updateTime' => time()
        ];
        return $this->table('userAddress')->where([
            'id'        => $id,
            'userId'    => $userId
        ])->update($data);
    }
}